package com.lehecai.core.api.event;

import com.lehecai.core.api.AbstractApiResultBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by zhangzq on 2015/2/9.
 */
public class QuestionAnswerBean extends AbstractApiResultBean {

    private Integer id;
    private String question;
    private String answer;

    public static QuestionAnswerBean convertFromJSONObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        QuestionAnswerBean questionAnswerBean = new QuestionAnswerBean();
        questionAnswerBean.setAnswer(getString(object, "answer"));
        questionAnswerBean.setId(getInt(object, "id"));
        questionAnswerBean.setQuestion(getString(object, "question"));
        return questionAnswerBean;
    }

    public static List<QuestionAnswerBean> convertFromJSONArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        List<QuestionAnswerBean> list = new ArrayList<QuestionAnswerBean>();
        for (Iterator<?> iterator = array.iterator(); iterator.hasNext();) {
            JSONObject object = (JSONObject) iterator.next();
            JSONArray jsonArray = new JSONArray();
            if (object.containsKey("data")) {
                jsonArray = object.getJSONArray("data");
            }
            if (jsonArray == null) {
                return null;
            }
            for (Iterator<?> iterator1 = jsonArray.iterator(); iterator1.hasNext();) {
                JSONObject objects = (JSONObject) iterator1.next();
                list.add(convertFromJSONObject(objects));
            }
        }
        return list;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

}
